package textdiff {

	/**
	 * A contiguous block of one or more lines with the same state, eg match or other.
	 */
	public class LineBlock {
	    public var fromLineNum: int;
	    public var thruLineNum: int;
	    public var lines: Array;
	    public var reportable: Boolean = false;
	
	    public function LineBlock(sourceLines:Array=null, fromLineNum:int=0, thruLineNum:int=16777214) {
	        this.fromLineNum = fromLineNum;
	        this.thruLineNum = thruLineNum;
	        
	        
	        /*
	        Original sourcecode (Java) for getting the line(s) for this line block:
	        
	        lines = new String[thruLineNum - fromLineNum + 1];
			System.arraycopy( sourceLines, fromLineNum, lines, 0, lines.length );
	        
	        Javadoc for System.arraycopy:
	        public static void arraycopy(Object source, int sourcePosition, 
				 Object destination, int destinationPosition, int numberOfElements)

	        System.arraycopy() copies numberOfElements elements from the array source, 
	        beginning with the element at sourcePosition, to the array destination starting at destinationPosition. 
	        The destination array must already exist when System.arraycopy() is called. The method does not create it. 
	        The source and destination arrays must be of the same type.
	        
	        example:
	        source = {1,2,3,4,5}
	        fromLineNum = 1
	        thruLineNum = 4
	        numberOfElements = thruLineNum - fromLineNum + 1 = 3
	        dest = new Integer[numberOfElements]
	        System.arraycopy(source, fromLineNum, dest, dest.length)
	        result: dest = {2,3,4}

			*/

			/* 
			Get the lines for this line block using Array.slice(...)
			
			ASDoc for Array.slice(...):
			function slice(startIndex:int = 0, endIndex:int = 16777215):Array
			Returns a new array that consists of a range of elements from the original array, 
			without modifying the original array. The returned array includes the startIndex element and all 
			elements up to, but not including, the endIndex element. 

			example:
	        source = {1,2,3,4,5}
	        fromLineNum = 1
	        thruLineNum = 3
	        dest = source.slice(fromLineNum, thruLineNum)
	        result: dest = {2,3}
	        
	        In short, slice does not include source[thruLineNum] in the copy. 
	        So we need to add 1 to thruLineNum
	        
	        */
	        if (sourceLines!=null) {
	        	lines = sourceLines.slice(fromLineNum, thruLineNum+1);
	        }
	    }
	    
	    public function toString(): String {
	    	var s:String = "line " + this.fromLineNum + " to " + this.thruLineNum + ":"+ textLines;
	    	return s;
	    }
	    
	    public function get textLines(): String {
	    	var s:String = "";
	    	for (var i:int=0; i<lines.length; i++) {
	    		s += lines[i] + "\r";
	    	}	    	
	    	return s;
	    }
	    
   	    public function toXML():XML {
	    	var result:XML = <lines from={fromLineNum} thru={thruLineNum}></lines>;
	    	for (var i:int=0; i<lines.length; i++) {
	    		var newnode:XML = <line>{lines[i]}</line>
				result.appendChild(newnode);
	    	}
	    	return result;
	    }

	}
}
